<template>
  <div class="container">
    <el-form ref="form" :model="form" :rules="rules" label-width="210px">
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="form.name"
            placeholder="请填写名称"
            clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="访问key" prop="accessKey">
        <el-input
            v-model="form.accessKey"
            readonly
        ></el-input>
      </el-form-item>
      <el-form-item label="密钥" prop="secretKey">
        <el-input
            v-model="form.secretKey"
            readonly
        ></el-input>
      </el-form-item>
      <el-form-item label="超时时间（单位：秒）" prop="timeout">
        <el-input
            v-model.number="form.timeout"
            placeholder="请填写数字"
            oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
            clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="凭证类型" prop="certType">
        <el-select
            v-model="form.certType"
            placeholder="请填写凭证类型"
            class="handle-select mr10"
            clearable
        >
          <el-option
              v-for="item in $enum.getMapItems('certType')"
              :key="item.value"
              :label="item.name"
              :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
            v-model="form.status"
            placeholder="请填写状态"
            class="handle-select mr10"
            clearable
        >
          <el-option
              v-for="item in $enum.getMapItems('status')"
              :key="item.value"
              :label="item.name"
              :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否内置" prop="buildInFlag">
        <el-switch v-model="form.buildInFlag" active-color="#13ce66"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
            size="small"
            v-model="form.remark"
            placeholder="请填写备注"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            show-word-limit
            maxlength="99"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="saveEdit"
        >保存
        </el-button
        >
        <el-button size="small" @click="goBack">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  appCertDetailApi,
  appCertEditApi,
  appCertSaveApi,
} from "../../../api/mgt/system/appCert";
import {createObjectBase, deepCopy} from "@/utils";

const obj = {
  //信息
  id: "",
  name: "",
  accessKey: "",
  secretKey: "",
  timeout: "",
  certType: "",
  status: "",
  buildInFlag: false,
  remark: "",
};

export default {
  data() {
    return {
      form: deepCopy(obj),
      rules: {
        name: [
          {required: true, message: "请填写API名称", trigger: "change"},
          {max: 50, message: "长度不能超过50个字符", trigger: "change"},
        ],
        certType: [
          {required: true, message: "请填写凭证类型", trigger: "change"},
        ],
        remark: [
          {max: 99, message: "长度不能超过99个字符", trigger: "change"},
        ],
      },
    };
  },
  methods: {
    // 编辑操作
    getDetail(id) {
      if (!id) {
        if (this.$refs.form != undefined) {
          this.$refs['form'].resetFields();
        }
      } else {
        appCertDetailApi({id}).then((res) => {
          const data = deepCopy(res?.bizData);
          this.form = data;
        });
      }
    },
    saveEdit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const data = createObjectBase(this.form, Object.keys(obj));
          const {id} = data;
          const fn = id ? appCertEditApi : appCertSaveApi;
          fn(data).then((res) => {
            this.goBack();
          });
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$router.goPage({
        path: "/appCert/page",
      });
    },
  },
  created() {
    const {id} = this.$route.query;
    this.getDetail(id);
  },
};
</script>
